/*
 * Copyright (c) 2016. 速位中国
 */

package com.grpc_server.grpc_controller;
/*

                  写字楼里写字间，写字间里程序员；
                  程序人'猿'写程序，又拿程序换酒钱。
                  酒醒只在网上坐，酒醉还来网下眠；
                  酒醉酒醒日复日，网上网下年复年。
                  但愿老死电脑间，不愿鞠躬老板前；
                  奔驰宝马贵者趣，公交自行程序员。
                  别人笑我忒疯癫，我笑自己命太贱；
                  不见满街漂亮妹，哪个归得程序'猿'？
━━━━━━神兽出没━━━━━━
 ┏┓   ┏┓
┏┛┻━━━┛┻┓
┃       ┃
┃   ━   ┃
┃ ┳┛ ┗┳ ┃
┃       ┃
┃   ┻   ┃
┃       ┃
┗━┓   ┏━┛
  ┃   ┃                    神兽保佑, 永无BUG!
  ┃   ┃       Code is far away from bug with the animal protecting
  ┃   ┗━━━┓
  ┃       ┣┓
  ┃       ┏┛
  ┗┓┓┏━┳┓┏┛
   ┃┫┫ ┃┫┫
   ┗┻┛ ┗┻┛
 */

import com.google.protobuf.TextFormat;
import com.grpc_server.grpc_controller.grpc_abstract_controller.AbstractController;
import io.grpc.stub.StreamObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import pb.GreeterGrpc;
import pb.HelloRequest;
import pb.HelloResponse;

/**
 * Created by 石头哥哥 on 1/12/16.
 * <p>
 * grpc
 * <p>
 * server.iml
 * <p>
 * comments:  测试 grpc handler
 */
@Service
public class GreeterController extends AbstractController implements GreeterGrpc.Greeter {

    /**
     * logger
     */
    private final Logger logger = LoggerFactory.getLogger(GreeterController.class);

    /**
     * @param request          上行 消息
     * @param responseObserver a response observer,
     *                         which is a special interface for the server
     *                         to call with its response
     */
    @Override
    public void sayHello(HelloRequest request,
                         StreamObserver<HelloResponse> responseObserver) {

        HelloResponse reply = HelloResponse.newBuilder().setMessage("接收到来自服务器测试响应 " + "grpc test response")
                .build();

        logger.debug("recvie data:" + TextFormat.printToUnicodeString(reply));

        responseObserver.onNext(reply);
        // response the client
        responseObserver.onCompleted();
    }

    /**
     * bind service
     */
    @Override
    public void bindService() {
        mutableHandlerRegistry.addService(GreeterGrpc.bindService(this));
    }
}
